package com.wolves.career.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wolves.career.entity.RealTimeForumTopic;
import com.wolves.career.service.RealTimeForumTopicService;
import com.wolves.career.utils.R;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.RequestScope;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 麦翔盛
 * @since 2021-10-27
 */
@RestController
@RequestMapping("/forum")
@CrossOrigin
public class RealTimeForumTopicController {
    @Autowired
    RealTimeForumTopicService topicService;

    /**
     * 添加帖子
     * @param topic
     * @return
     */
    @PostMapping("/addTopic")
    public R addTopic(@RequestBody RealTimeForumTopic topic){
        topicService.save(topic);
        return R.ok().data("success",true);
    }

    /**
     * 根据帖子id删除帖子
     * @param topicId
     * @return
     */
    @DeleteMapping("/deleteTopic/{topicId}")
    public R deleteTopic(@PathVariable String topicId){
        topicService.removeById(topicId);
        return R.ok().data("success",true);
    }

    /**
     * 修改论坛信息
     * @param topic
     * @return
     */
    @PutMapping("/updateTopic")
    public R updateTopic(@RequestBody RealTimeForumTopic topic){
        topicService.updateById(topic);
        return R.ok().data("success",true);
    }

    /**
     * 管理员分页查询【所有】帖子
     * @param topic 帖子的封装类
     * @param pageNo 当前页码
     * @param limit 每页限制条数
     * @return
     */
    @PostMapping("/listTopic/{pageNo}/{limit}")
    public R listTopic(@RequestBody RealTimeForumTopic topic, @PathVariable long pageNo, @PathVariable long limit){
        QueryWrapper<RealTimeForumTopic> queryWrapper = topicService.getTopicQueryWrapper(topic);
        Page<RealTimeForumTopic> page = new Page<>(pageNo, limit);
        IPage<RealTimeForumTopic> topicIPage = topicService.page(page,queryWrapper);
        return R.ok().data("records",topicIPage.getRecords()).data("total",topicIPage.getTotal());
    }

    /**
     * 普通用户分页查询【已经通过审核】的帖子
     * @param topic 帖子的封装类
     * @param pageNo 当前页码
     * @param limit 每页限制条数
     * @return
     */
    @PostMapping("/listTopicPass/{pageNo}/{limit}")
    public R listTopicPass(@RequestBody RealTimeForumTopic topic, @PathVariable long pageNo, @PathVariable long limit){
        QueryWrapper<RealTimeForumTopic> queryWrapper = topicService.getTopicQueryWrapper(topic);
        queryWrapper.eq("is_pass",1);
        Page<RealTimeForumTopic> page = new Page<>(pageNo, limit);
        IPage<RealTimeForumTopic> topicIPage = topicService.page(page,queryWrapper);
        return R.ok().data("records",topicIPage.getRecords()).data("total",topicIPage.getTotal());
    }

    /**
     * 维护人员审核帖子
     * @param topicId
     * @param isPass 审核信息
     * @return
     */
    @PostMapping("/auditTopic/{topicId}/{isPass}")
    public R auditTopic(@PathVariable String topicId, @PathVariable Integer isPass){
        //先根据文章id获取文章
        RealTimeForumTopic topic = topicService.getById(topicId);
        //修改审核信息
        topic.setIsPass(isPass);
        //修改帖子审核信息
        topicService.updateById(topic);
        return R.ok().data("success",true);
    }

}

